﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.IO;
using System.Resources;

namespace Tesla.Content {
    /// <summary>
    /// Represents a resource embedded in an assembly.
    /// </summary>
    public class EmbeddedResource : IResource {
        private ResourceManager _resourceManager;
        private String _extension;
        private String _name;
        private String _fullName;

        /// <summary>
        /// Gets the extension of the asset.
        /// </summary>
        public String Extension {
            get {
                return _extension;
            }
        }

        /// <summary>
        /// Gets the name of the asset (without extension).
        /// </summary>
        public String Name {
            get {
                return _name;
            }
        }

        /// <summary>
        /// Gets the full name of the asset (including name and extension).
        /// </summary>
        public String FullName {
            get {
                return _fullName;
            }
        }

        /// <summary>
        /// Creates a new embedded resource.
        /// </summary>
        /// <param name="resourceManager">ResourceManager the resource is managed by</param>
        /// <param name="embeddedResourceName">Name of embdedded resource</param>
        public EmbeddedResource(ResourceManager resourceManager, String embeddedResourceName) {
            _resourceManager = resourceManager;
            _extension = Path.GetExtension(embeddedResourceName).ToLower();
            _name = Path.GetFileNameWithoutExtension(embeddedResourceName);
            _fullName = embeddedResourceName;
        }

        /// <summary>
        /// Opens a stream to read from the embdedded resource.
        /// </summary>
        /// <returns>Memory stream</returns>
        /// <exception cref="System.IO.IOException">Thrown if the resource does not exist.</exception>
        public Stream OpenStream() {
            Object obj = _resourceManager.GetObject(_name);
            if(obj is byte[]) {
                byte[] bytes = obj as byte[];
                return new MemoryStream(bytes);
            } else if(obj is String) {
                String s = obj as String;
                MemoryStream ms = new MemoryStream();
                StreamWriter writer = new StreamWriter(ms);
                writer.Write(s);
                writer.Flush();
                return ms;
            } else {
                throw new IOException("Embedded resource does not exist");
            }
        }

    }
}
